/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.engcomp.mpc.util;

import org.apache.commons.math.linear.ArrayRealVector;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.RealVector;
import org.ojalgo.matrix.BasicMatrix;
import org.ojalgo.matrix.PrimitiveMatrix;
import org.ojalgo.matrix.store.MatrixStore;

/**
 *
 * @author Felipe
 */
public class OjAlgoUtils {

    public static RealVector convertOjalgoToRealVector(BasicMatrix matrix) {
        RealVector vector = null;
        
        if (matrix.isVector()) {
            vector = new ArrayRealVector(matrix.size());
            
            for (int i=0; i<matrix.size(); i++) {
                if (matrix.getColDim() == 1) {
                    vector.setEntry(i, matrix.doubleValue(i, 0));
                } else {
                    vector.setEntry(i, matrix.doubleValue(0, i));
                }
            }
        }
        
        return vector;
    }

    public static MatrixStore convertRealMatrixToOjalgo(RealMatrix matrix) {
        return PrimitiveMatrix.FACTORY.rows(matrix.getData()).toPrimitiveStore();
    }

    public static MatrixStore convertColumnRealVectorToOjalgo(RealVector vector) {
        return PrimitiveMatrix.FACTORY.columns(vector.getData()).toPrimitiveStore();
    }

}
